package com.dachuang.canjiren.service.impl;

import com.dachuang.canjiren.entity.User;
import com.dachuang.canjiren.entity.Volunteer;
import com.dachuang.canjiren.mapper.UserMapper;
import com.dachuang.canjiren.mapper.VolunteerMapper;
import com.dachuang.canjiren.service.VolunteerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dachuang.common.exception.BizCodeEnume;
import com.dachuang.common.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-27
 */
@Service
public class VolunteerServiceImpl extends ServiceImpl<VolunteerMapper, Volunteer> implements VolunteerService {

        @Autowired
        private VolunteerMapper volunteerMapper;
        @Override
        public R selectInfo(Integer id) {

            if (Objects.isNull(id)){
                return R.error(BizCodeEnume.ERROR20005);
            }
            Volunteer volunteer = volunteerMapper.selectInfo(id);
            if (Objects.isNull(volunteer)){
                return R.error(BizCodeEnume.ERROR40001);
            }
            R r = R.ok();
            r.put("data",volunteer);
            return r;
        }


    @Override
    public R insertVolunteer(String theme,String content,String openTime,String endTime,String sponsor) {

        if (StringUtils.isEmpty(theme)){
            return R.error(BizCodeEnume.ERROR30001);
        }
        if (StringUtils.isEmpty(content)){
            return R.error(BizCodeEnume.ERROR30002);
        }
        if (StringUtils.isEmpty(openTime)){
            return R.error(BizCodeEnume.ERROR80001);
        }
        if (StringUtils.isEmpty(sponsor)){
            return R.error(BizCodeEnume.ERROR80002);
        }
        if (StringUtils.isEmpty(endTime)){
            return R.error(BizCodeEnume.ERROR80003);
        }

        Volunteer volunteer = new Volunteer();
        volunteer.setContent(content);
        volunteer.setTheme(theme);
        volunteer.setSponsor(sponsor);
        volunteer.setOpenTime(openTime);
        volunteer.setEndTime(endTime);

        if (volunteerMapper.insertVolunteer(volunteer) > 0){
            return R.ok();
        }
        return R.error();
    }

    @Override
    public R selectVolunteerList(Integer pageNum,Integer pageSize,String keyword) {
        if (Objects.isNull(pageNum)){
            pageNum = 1;
        }
        if (Objects.isNull(pageSize)){
            pageSize = 10;
        }

        PageHelper.startPage(pageNum,pageSize);
        List<Volunteer> volunteers = volunteerMapper.selectList(keyword);
        PageInfo pageInfo = new PageInfo<>(volunteers);
        R r = R.ok();
        r.put("data",pageInfo);
        return r;
    }
}
